package ins.wink.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ins.wink.entity.Goods;
import ins.wink.entity.GoodsStore;
import ins.wink.entity.History;
import ins.wink.entity.Store;
import ins.wink.entity.Userinfo;
import ins.wink.service.GoodsService;
import ins.wink.service.HistoryService;
import ins.wink.service.StoreService;
import ins.wink.utils.MD5Utils;

/**
 * 库层controller层
 */
@Controller
public class GoodsController {
	//注入service层
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private StoreService storeService;
	@Autowired
	private HistoryService historyService;
	
	//查所有跳转货物库存
	@RequestMapping(value="/goodsAction_searchUI",method=RequestMethod.GET)
	public String goodsAction_searchUI(HttpServletRequest request) {
		List<GoodsStore> list = goodsService.goodsStoreAlls();
		request.setAttribute("list", list);
		return "WEB-INF/goods/remain";
	}
	
	////查询通过条件 简记码  名称  仓库Id
	@RequestMapping(value="/goodsAction_searchUI1",method=RequestMethod.GET)
	public String jjmsGoods(Model model,Goods goods) {
		//获取简记码
		String nm = goods.getNm();
		//获取名称
		String goodname = goods.getGoodname();
		//获取仓库编号
		String storeid = goods.getStoreid();
		//执行方法
		List<GoodsStore> list = goodsService.tjSelect(nm,goodname,storeid);
		//数据回显页面上
		model.addAttribute("list",list);
		model.addAttribute("nm",nm);
		model.addAttribute("goodname",goodname);
		model.addAttribute("storeid",storeid);
		return "WEB-INF/goods/remain";
	}
	//查store仓库表goodsAction_findAllStore
	@RequestMapping(value="/goodsAction_findAllStore",method=RequestMethod.POST)
	@ResponseBody
	public List<Store> goodsAction_findAllStore(HttpServletRequest request){
		List<Store> list = storeService.storeAlls();
		return list;
	}
	/**
	 * 入库
	 */
	//简记码查询一
	@RequestMapping(value="/checkByStoreNm",method=RequestMethod.POST)
	@ResponseBody
	public List<Goods> checkByStoreNm(@RequestParam(value="nm")String nm) {
		//查询goods表
		List<Goods>list = goodsService.checkByStoreNms(nm);
		return list;
	}
	
	//入库	
	@RequestMapping(value="/goodsAction_addUI")
	public String goodsAction_addUI(HttpServletRequest request) {
		return "WEB-INF/goods/save";
	}
	//已有物品编号进行修改
	@RequestMapping(value="/updategood",method=RequestMethod.POST)
	@ResponseBody
	public boolean updategood(Map<String, Object>map, String id,double num,HttpServletRequest request) {
		//获取用户
		Userinfo user = (Userinfo) request.getSession().getAttribute("user");
		//查询剩余的物品数量
		double d = goodsService.goodsAmounts(id);
		System.out.println(d);
		//入库数量加起来
		double amount = d+num;
		System.out.println(amount);
		
		Map<String, Object>map1 = new HashMap<String, Object>();
		map1.put("id", id);
		map1.put("amount", amount);
		//执行goods修改操作
		int i = goodsService.updateGoodss(map1);
		/*
		 * 添加入库历史记录
		 */
		//实例化历史记录对象history
		History history = new History();
		String historyId = MD5Utils.md5(UUID.randomUUID().toString());
		history.setId(historyId);
		history.setAmount(num);
		history.setRemain(amount);
		history.setHistoryid(id);
		history.setUser(user.getUsername());
		historyService.addHistorys(history);
		
		
		if(i>0){
			return true;
		}else {
			return false;
		}
	}
	//物品编号不存在进行添加
	@RequestMapping(value="/addgood",method=RequestMethod.POST)
	@ResponseBody
	public boolean addgood(Goods goods,HttpServletRequest request) {
		//获取随机数并加密
		String id = MD5Utils.md5(UUID.randomUUID().toString());
		//实例化新的goods对象
		Goods goods1 = new Goods();
		
		goods1.setId(id);
		goods1.setNm(goods.getNm());
		goods1.setGoodname(goods.getGoodname());
		goods1.setUnit(goods.getUnit());
		goods1.setAmount(goods.getAmount());
		goods1.setStoreid(goods.getStoreid());
		int i = goodsService.addGoods(goods1);
		
		/*
		 * 添加入库历史记录
		 */
		//获取user对象
		Userinfo user = (Userinfo) request.getSession().getAttribute("user");
		//实例化历史记录对象history
		History history = new History();
		String historyId = MD5Utils.md5(UUID.randomUUID().toString());
		history.setId(historyId);
		history.setAmount(goods.getAmount());
		history.setRemain(goods.getAmount());
		history.setHistoryid(id);
		history.setUser(user.getUsername());
		historyService.addHistorys(history);
		
		if(i!=0) {
			return true;
		}else {
			return false;
		}
	}
	/**
	 * 出库
	 */
	@RequestMapping(value="/goodsAction_outUI")
	public String goodsAction_outUI() {
		return "WEB-INF/goods/out";
	}
	
	//简记码查询二
		@RequestMapping(value="/checkByStoreNm2",method=RequestMethod.POST)
		@ResponseBody
		public List<Goods> checkByStoreNm2(@RequestParam(value="nm")String nm) {
			//查询goods表
			List<Goods>list = goodsService.checkByStoreNms(nm);
			return list;
	}
	//出库时数量够的花执行修改goodsAction_out
		@RequestMapping(value="/goodsAction_out",method=RequestMethod.POST)
		@ResponseBody
		public boolean updategood1(Map<String, Object>map, String id,double num,HttpServletRequest request) {
			//先根据id查询单条数据来过去剩余的数量
			Goods g = goodsService.selectGoodsByIds(id);
			double d = g.getAmount();
			System.out.println(d+"=d");
			if(d>num) {//当剩余数量大于出库时
				System.out.println(">>>>");
				Map<String, Object>map1 = new HashMap<String, Object>();
				map1.put("id", id);
				map1.put("amount", d-num);
				int i = goodsService.updateGoodss(map1);
				
				/*
				 * 添加出库历史记录
				 */
				//获取用户信息
				Userinfo user = (Userinfo) request.getSession().getAttribute("user");
				//实例化history对象
				History history = new History();
				//随机数MD5加密
				String uuid = MD5Utils.md5(UUID.randomUUID().toString());
				history.setId(uuid);
				history.setAmounts(num);
				history.setRemain(d-num);
				history.setHistoryid(id);
				history.setUser(user.getUsername());
				historyService.updateHistorys(history);
				
				if(i>0){
					return true;
				}
			}else if(d==num) {//出库数量等于仓库剩余数量执行删除
				int i = goodsService.deleteGoodss(id);
				if(i>0){
					return true;
				}
			}else if(d<num) {//出库数量大于剩余数量
				return false;
			}
			return false;
		}
		
}

